package com.wnhz.work.yz.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.RecordsItemBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AddRecordsCodeActivity extends BaseActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_records_code);
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("选择备案");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        TextView submit = (TextView) findViewById(R.id.submit);
        submit.setVisibility(View.GONE);
        submit.setOnClickListener(this);
        initView();
        initData();
    }

    private RecyclerView cluesRecycler;
    private List<RecordsItemBean.InfoEntity> info = new ArrayList<>();
    private RecordAdapter adapter;

    private void initView() {
        cluesRecycler = (RecyclerView) findViewById(R.id.clues_recycler);
        cluesRecycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
    }


    private void initData() {
        upload();
    }

    @Override
    public void onResume() {
        upload();
        super.onResume();
    }

    private void upload() {
        showDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("type", "1");
        XUtil.Post(Url.API_RECORDLIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e("----", result);
                Gson gson = new Gson();
                RecordsItemBean bean = gson.fromJson(result, RecordsItemBean.class);
                if (bean.getResult().equals("1")) {
                    info = bean.getInfo();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
                Log.e("---", info.size() + "");
                adapter = new RecordAdapter(AddRecordsCodeActivity.this, info);
                cluesRecycler.setAdapter(adapter);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                super.onError(ex, isOnCallback);
            }
        });
    }

    class RecordAdapter extends BaseRVAdapter {
        private List<RecordsItemBean.InfoEntity> info;

        /**
         * 构造器
         *
         * @param context 上下文
         * @param list    数据集合
         */
        public RecordAdapter(Context context, @NonNull List list) {
            super(context, list);
            info = list;
        }

        @Override
        public int getLayoutId(int viewType) {
            return R.layout.item_records;
        }

        @Override
        public void onBind(BaseViewHolder holder, final int position) {
            holder.setTextView(R.id.name, info.get(position).getName());
            holder.setTextView(R.id.product_type, info.get(position).getType());
            holder.setTextView(R.id.nums, info.get(position).getNums());
            holder.setTextView(R.id.max_money, info.get(position).getBudget());
            holder.setTextView(R.id.time, info.get(position).getCreate_at());
            holder.setTextView(R.id.address, info.get(position).getAddress());
            holder.getView(R.id.rl_max).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent();
                    i.putExtra("recordsId", info.get(position).getRecordid());
                    i.putExtra("recordsName", info.get(position).getName());
                    setResult(RESULT_OK, i);
                    finish();
                }
            });
        }

        private Intent intent;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

        }
    }
}
